<?php

declare(strict_types=1);

namespace app\common\model;

class Expert extends Base
{

    protected $name = 'expert';

    protected $type = [
        'confirm_time' => 'timestamp',
    ];
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,photo,mobile,level_id');
    }
    public function getStatusTextAttr($value, $data)
    {
        return match ($data['status']) {
            1 => '待审核',
            2 => '已通过',
            3 => '已驳回',
            default => ''
        };
    }

    /**
     * 申请资料
     */
    public static function add($param, $user)
    {
        try {
            $info = parent::where('uid', $user->id)->find();
            if (!empty($info)) {
                if ($param['type'] != $info->getData('type')) {
                    return ['code' => -1, 'msg' => '身份类型不允许修改'];
                }
                if ($info->status == 3) {
                    $param['status'] = 1;
                    parent::where(['id' => $info->id])->update($param);
                    return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核'];
                } else if ($info->status == 1) {
                    return ['code' => -1, 'msg' => '您的申请还在审核中'];
                } else {
                    parent::where(['id' => $info->id])->update($param);
                    return ['code' => 1, 'msg' => '修改成功'];
                }
            } else {
                $param['uid'] = $user->id;
                parent::create($param);
                return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核'];
            }
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 审核
     */
    public static function audit($param, $logs)
    {

        parent::startTrans();
        try {
            if (!isset($param['status'])) return ['code' => -1, 'msg' => '请选择审核类型'];

            $info = parent::where('id', $param['id'])->find();
            if (empty($info)) return ['code' => -1, 'msg' => '审核失败'];
            if ($info->status != 1) {
                return ['code' => -1, 'msg' => '此笔记录已审核，请勿重复审核！'];
            }
            $level_title = Level::where(['id' => $info['type']])->value('title');
            if ($param['status'] == 2) {
                $msg = '通过' . $level_title . '申请，ID【' . $param['id'] . '】';
                User::where(['id' => $info['uid']])->update(['level_id' => $info['type'], 'job' => $info['job']]);
            } else {
                $msg = '拒绝' . $level_title . '申请,ID【' . $param['id'] . '】';
            }

            $info->status = $param['status'];
            if ($param['status'] != 2) {
                $info->reject_msg = $param['reject_msg'];
            }
            $info->confirm_time = time();
            $info->admin_id = $logs->id;
            $info->admin_name = $logs->admin_name;
            $info->save();
            Logs::adds($msg, $logs->id, $logs->admin_name);
            parent::commit();
            return ['code' => 1, 'msg' => '已审核'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
